package com.matrix.draft.rabbitmqspringboot.rabbitmq.consumer;

import cn.hutool.core.lang.Console;
import com.matrix.draft.rabbitmqspringboot.config.RabbitMQConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

/**
 * 死信队列消费者
 */
//@Component
public class DeadQueueConsumer {

    public static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd HH:mm:ss").toFormatter();

    @Autowired
    Environment env;

    @RabbitListener(queues = RabbitMQConfig.DEAD_QUEUE)
    void receivingDeadQueue(Message message) {
        String body = new String(message.getBody(), Charset.forName(env.getProperty("spring.rabbitmq.encoding")));
        Console.log("[{}]接收到死信消息:{}\t", LocalDateTime.now().format(FORMATTER), body, message.getMessageProperties().getDelay());
    }
}
